package com.CST.service.impl.cb;

import com.CST.entity.cb.BdSysMenu;
import com.CST.mapper.cst.cb.BdSysMenuMapper;
import com.CST.mapper.cst.cb.BdSysRoleMapper;
import com.CST.mapper.cst.cb.BdSysRoleMenuMapper;
import com.CST.mapper.cst.cb.BdSysUserMapper;
import com.CST.service.cb.IBdEnumdetailService;
import com.CST.service.cb.IBdSysMenuService;
import com.CST.vo.cb.BdSysMenuInfoVO;
import com.CST.vo.cb.BdSysMenuVO;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>菜单 服务实现类</p>
 *
 * @author: y
 * @create: 2021-09-27 14:48
 **/
@Service
public class BdSysMenuServiceImpl extends ServiceImpl<BdSysMenuMapper, BdSysMenu> implements IBdSysMenuService {

    @Autowired
    private IBdEnumdetailService enumdetailService;

    @Autowired
    private BdSysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private BdSysUserMapper sysUserMapper;

    @Autowired
    private BdSysRoleMapper sysRoleMapper;

    /**
     * 获取菜单信息
     *
     * @param id
     * @return java.util.List<com.CST.entity.cb.BdSysMenu>
     * @author y
     * @since 2021/09/27 15:13
     */
    @Override
    public BdSysMenu getSyeMenu(String id) {
        return baseMapper.getSysMenuParentId(id);
    }

    /**
     * 新建菜单
     *
     * @param bdSysMenu
     * @return com.CST.common.api.Response
     * @author y
     * @since 2021/09/27 15:52
     */
    @Override
    public boolean insertSysMenu(BdSysMenu bdSysMenu) {
        List<BdSysMenu> sysMenuList = baseMapper.getSyeMenu(bdSysMenu.getMenuName());
        if (null != sysMenuList && sysMenuList.size() > 0) {
            return false;
        }
        bdSysMenu.setCreateTime(new Date());
        bdSysMenu.setDelFlag("0");
        //如果前端没传父级id，默认赋值为0
        if (StringUtil.isEmpty(bdSysMenu.getParentId())) {
            bdSysMenu.setParentId("0");
        }
        return baseMapper.insert(bdSysMenu) > 0;

    }

    /**
     * 更新菜单表
     *
     * @param bdSysMenu
     * @return int
     * @author y
     * @since 2021/09/27 17:06
     */
    @Override
    public boolean updateSysMenu(BdSysMenu bdSysMenu) {
        // bdSysMenu.setUpdateTime(new Date());
        return baseMapper.updateById(bdSysMenu) == 1;
    }

    /**
     * <p>获取角色下菜单所有</p >
     *
     * @param roleId
     * @return java.lang.Object
     * @author y
     * @since 2021/10/13 16:59
     */
    @Override
    public Object getSysMenuListByRole(String roleId) {
        //获取角色的菜单信息
        //获取角色id
        //BdSysRoleMenu sysRoleMenu = new BdSysRoleMenu();
        /*List<BdSysRoleMenu> roles = sysRoleMenuMapper.getSysRoleMenuByRoleId(roleId);
        List<String> menuIdList =roles.stream().map(BdSysRoleMenu::getMenuId).collect(Collectors.toList());
        List<BdSysMenu> selectList  =baseMapper.getSysMenu();
        List<BdSysMenu> newMenuList = selectList.stream().filter(t->menuIdList.contains(t.getId())).collect
        (Collectors.toList());*/
        roleId = transformString(roleId);
        List<BdSysMenu> menus = sysRoleMenuMapper.selectRoleMenuById(roleId);


//        for (int j = 0; j < roles.size(); j++) {
//            BdSysMenu sysMenus = baseMapper.getSysMenuParentId(roles.get(j).getMenuId());
//            newMenuList.add(sysMenus);
//        }

        BdSysMenu root = new BdSysMenu();
        root.setId("0");
        root.setMenuName("root");

//        //获取默认规则
//        BdEnumdetail searchInfo = new BdEnumdetail();
//        searchInfo.setEdEdId(1);//院区值
//
//        BdEnumdetail rule = enumdetailService.getDefaultCode(searchInfo);
//        if (rule != null) {
//            System.setProperty("ruleCode",rule.getEdCode());
//        }
//        else
//        {
//            System.setProperty("ruleCode","0");
//        }
        return treePower(menus, root);
    }


    /**
     * <p>获取全部菜单</p >
     *
     * @return java.lang.Object
     * @author y
     * @since 2021/10/13 17:03
     */
    @Override
    public Object getSysMenu() {
        //获取所有菜单信息
        List<BdSysMenuVO> list = baseMapper.getSysMenu();
        return list.stream()
                .filter(e -> "0".equals(e.getParentId()))
                .peek(e -> e.setChildNodes(child(list, e)))
                .collect(Collectors.toList());
    }

    @Override
    public Object getSysMenuLike(String name) {
        if (StringUtils.isBlank(name)) {
            return getSysMenu();
        } else {
            return baseMapper.selectMenu(name);
        }
    }

    private static List<BdSysMenuVO> child(List<BdSysMenuVO> list, BdSysMenuVO e) {
        return list.stream()
                .filter(f -> f.getParentId().equals(e.getId()))
                .peek(p -> p.setChildNodes(child(list, p)))
                .collect(Collectors.toList());
    }

    @Override
    public Object getMenuAll(String name) {
        return baseMapper.selectMenu(name);
    }

    @Override
    public Object getMenuInfo(String id) {
        List<BdSysMenuVO> list = baseMapper.selectMenu(null);
        BdSysMenuVO info = baseMapper.selectMenuInfo(id);
        List<BdSysMenuVO> strings = new ArrayList<>();
        strings.addAll(getPid(list, info, strings));
        strings.add(info);
        List<String> arrayList = new ArrayList<>();
        strings.forEach(e -> arrayList.add(e.getId()));
        return new BdSysMenuInfoVO().setIds(arrayList).setPath(info.getPath()).setMenuName(info.getMenuName());
    }

    private static List<BdSysMenuVO> getPid(List<BdSysMenuVO> list, BdSysMenuVO e, List<BdSysMenuVO> strings) {
        return list.stream()
                .filter(f -> f.getId().equals(e.getParentId()))
                .peek(p -> strings.addAll(getPid(list, p, strings)))
                .collect(Collectors.toList());
    }

    public static List<BdSysMenu> treePower(List<BdSysMenu> list, BdSysMenu bdSysMenu) {

        List<BdSysMenu> children = new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getParentId().equals(bdSysMenu.getId())) {
                treePower(list, list.get(i));
                children.add(list.get(i));
            }
        }
        bdSysMenu.setChildNodes(children);
        return children;
    }

    public static String transformString(String input) {
        // 去除首尾空格
        input = input.trim();

        String[] splitList = input.split(",");

        // 使用正则表达式替换逗号和数字
        StringBuilder sb = new StringBuilder("(");
        for (int i = 0; i < splitList.length; i++) {
            if (i < splitList.length - 1) {
                sb.append("'" + splitList[i] + "',");
            } else {
                sb.append("'" + splitList[i] + "'");
            }

        }
        sb.append(")");

        return sb.toString();
    }
}
